package com.swsc.solarverse.common.mybatis;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swsc.solarverse.common.model.PageInfo;
import com.swsc.solarverse.common.util.PageUtils;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * @Author chencl
 * @Date 2022/1/18 15:02
 * @Version 1.0
 * @Description BasePlusMapper
 */
public interface BasePlusMapper<T> extends BaseMapper<T> {
    /**
     * mybatis plus page转换通用page
     * @param page
     * @param queryWrapper
     * @return
     */
    default PageInfo<T> selectByPage(PageInfo<T> page, Wrapper<T> queryWrapper) {
        IPage<T> pageQuery = new Page<>(page.getPageNo(), page.getPageSize());
        IPage<T> iPage = selectPage(pageQuery, queryWrapper);
        return PageUtils.pageConversion(iPage);
    }


    /**
     * mybatis plus page转换通用page
     * @param page
     * @param queryWrapper
     * @return
     */
    default PageInfo<Map<String, Object>> selectMapsPage(PageInfo<Object> page, Wrapper<T> queryWrapper) {
        IPage<Map<String, Object>> pageQuery = new Page<>(page.getPageNo(), page.getPageSize());
        IPage<Map<String, Object>> iPage = selectMapsPage(pageQuery, queryWrapper);
        return PageUtils.pageConversion(iPage);
    }


    /**
     * 批量插入
     * @param list 插入记录
     * @return 条数
     */
    int insertList(@Param("list") List<T> list);


    /**
     * 根据Wrapper条件，查询一条记录，并锁定
     * @param queryWrapper 实体对象封装操作类
     * @return
     */
    T selectOneForUpdate(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}
